//
//  NSObject+SWRuntime.m
//  QZMJ
//
//  Created by 曾少伟 on 2017/1/4.
//  Copyright © 2017年 曾少伟. All rights reserved.
//

#import "NSObject+SWRuntime.h"

@implementation NSObject (SWRuntime)

- (NSArray *)getPropertyList {
    NSMutableArray * propertyNameArr = [NSMutableArray array];
    unsigned int outCount = 0;
    objc_property_t * properties = class_copyPropertyList([self class], &outCount);
    for (unsigned int i = 0; i < outCount; i ++) {
        objc_property_t property = properties[i];
        //属性名
        const char * name = property_getName(property);
        NSString *propertyName = [NSString stringWithFormat:@"%s",name];
        [propertyNameArr addObject:propertyName];
    }
    free(properties);
    return propertyNameArr;
}

- (NSArray *)getPropertyClassNameList {
    NSMutableArray * propertyClassName = [NSMutableArray array];
    
    unsigned int outCount = 0;
    objc_property_t * properties = class_copyPropertyList([self class], &outCount);
    for (unsigned int i = 0; i < outCount; i ++) {
        objc_property_t property = properties[i];
        //属性的特性
        unsigned int attrCount = 0;
        objc_property_attribute_t * attrs = property_copyAttributeList(property, &attrCount);
        for (unsigned int j = 0; j < attrCount; j ++) {
            objc_property_attribute_t attr = attrs[j];
            const char * name = attr.name;
            [propertyClassName addObject:[NSString stringWithFormat:@"%s",name]];
//            const char * value = attr.value;
//            NSLog(@"属性的描述：%s 值：%s", name, value);
        }
        free(attrs);
    }
    free(properties);
    
    return propertyClassName;
}

- (void)aaa {
    unsigned int outCount = 0;
    objc_property_t * properties = class_copyPropertyList([self class], &outCount);
    for (unsigned int i = 0; i < outCount; i ++) {
        objc_property_t property = properties[i];
        //属性名
        const char * name = property_getName(property);
        //属性描述
        const char * propertyAttr = property_getAttributes(property);
        NSLog(@"属性描述为 %s 的 %s ", propertyAttr, name);
        
        //属性的特性
        unsigned int attrCount = 0;
        objc_property_attribute_t * attrs = property_copyAttributeList(property, &attrCount);
        for (unsigned int j = 0; j < attrCount; j ++) {
            objc_property_attribute_t attr = attrs[j];
            const char * name = attr.name;
            const char * value = attr.value;
            NSLog(@"属性的描述：%s 值：%s", name, value);
        }
        free(attrs);
        NSLog(@"\n");
    }
    free(properties);
}


@end
